package activity.Left.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.bumptech.glide.Glide;
import com.hichip.campro.R;
import com.hichip.sdk.PlayLocal;
import com.hichip.widget.stickygridheaders.StickyGridHeadersSimpleAdapter;
import android.content.Context;
import android.graphics.Point;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import custom.stickygridview.GridItem;
import custom.stickygridview.MyImageView;
import utils.HiTools;

public class StickyGridVideoAdapter extends BaseAdapter implements StickyGridHeadersSimpleAdapter {

    private List<GridItem> list;
    private LayoutInflater mInflater;
    private GridView mGridView;
    private Point mPoint = new Point(0, 0);
    int     delMode;// false =0     true =1
    public  boolean[] checks;
    private Context  mContext;
    private SimpleDateFormat sdf=new SimpleDateFormat("00:mm:ss");
    public PlayLocal playLocal;

    public StickyGridVideoAdapter(Context context, List<GridItem> list,GridView mGridView) {
        this.list = list;
        this.mContext=context;
        mInflater = LayoutInflater.from(context);
        this.mGridView = mGridView;
        checks=new boolean[list.size()];
        playLocal=new PlayLocal();
    }

    public void setDelMode(int delMode){
        this.delMode=delMode;
        for(int i  = 0 ;i < checks.length; i ++){
            checks[i] = false;
        }
        notifyDataSetChanged();
    }


    public int getDelMode(){
        return delMode;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public GridItem getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_grid_video, parent, false);
            mViewHolder.mImageView = (MyImageView) convertView.findViewById(R.id.grid_item);
            mViewHolder.grid_cb = (CheckBox) convertView.findViewById(R.id.grid_cb);
            mViewHolder.tv_start_time=convertView.findViewById(R.id.tv_start_time);
            mViewHolder.tv_duration=convertView.findViewById(R.id.tv_duration);
            convertView.setTag(mViewHolder);
            mViewHolder.mImageView.setOnMeasureListener(new MyImageView.OnMeasureListener() {
                @Override
                public void onMeasureSize(int width, int height) {
                    mPoint.set(width, height);
                }
            });

        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        GridItem gridItem=list.get(position);
        mViewHolder.grid_cb.setChecked(checks[position]);
        String path = list.get(position).getPath();
        if(gridItem.getType()==0){
            mViewHolder.mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            mViewHolder.mImageView.setPadding(0,15,15,0);
            mViewHolder.mImageView.setImageResource(R.mipmap.online);
        }else {
            mViewHolder.mImageView.setPadding(0,0,0,0);
            mViewHolder.mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(mContext).load(path).into(mViewHolder.mImageView);
        }

        String  strTime=getTime(gridItem.getPath());
        String [] arr = strTime.split("\\s+");
        if(arr.length>2){
            if(arr[2].equals("AM")){
                strTime=mContext.getString(R.string.am)+" "+arr[1];
            }else if(arr[2].equals("PM")){
                strTime=mContext.getString(R.string.pm)+" "+arr[1];
            }
            mViewHolder.tv_start_time.setText(strTime);
        }

//      mViewHolder.tv_start_time.setText(getTime(gridItem.getPath()));
        if(playLocal!=null){
            Date date=new Date(playLocal.GetFileTime(path)*1000);
            mViewHolder.tv_duration.setText(sdf.format(date));
        }
        if(delMode==1){
            mViewHolder.grid_cb.setVisibility(View.VISIBLE);
        }else{
            mViewHolder.grid_cb.setVisibility(View.GONE);
        }
        return convertView;
    }


    private String getTime(String path){
        if(!TextUtils.isEmpty(path)){
            String[] strs=path.split("/");
            String str=strs[strs.length-1];
            if(!TextUtils.isEmpty(str)){
                String[] strings=str.split("\\.");
                String string=strings[0];
                String[] strings1=string.split("_");
                String myStr=strings1[strings1.length-1];
                SimpleDateFormat sdf=new SimpleDateFormat("HHmmss");
                try {
                    Date date=sdf.parse(myStr);
                    sdf=new SimpleDateFormat(" KK:mm:ss aa",Locale.ENGLISH);
                    String string2=sdf.format(date);
                    return  string2;
                } catch (ParseException e) {
                    e.printStackTrace();
                    return  " ";
                }
            }
        }
        return  " ";
    }


    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder mHeaderHolder;
        if (convertView == null) {
            mHeaderHolder = new HeaderViewHolder();
            convertView = mInflater.inflate(R.layout.grid_header, parent, false);
            mHeaderHolder.mTextView = (TextView) convertView
                    .findViewById(R.id.grid_header);
            mHeaderHolder.mTextView = (TextView) convertView
                    .findViewById(R.id.grid_header);
            convertView.setTag(mHeaderHolder);
        } else {
            mHeaderHolder = (HeaderViewHolder) convertView.getTag();
        }

        mHeaderHolder.mTextView.setText(list.get(position).getTime());

        return convertView;
    }

    public static class ViewHolder {
        public MyImageView mImageView;
        public CheckBox grid_cb;
        public TextView tv_start_time;
        public TextView tv_duration;
    }

    public static class HeaderViewHolder {
        public TextView mTextView;
    }

    @Override
    public long getHeaderId(int position) {
        return list.get(position).getSection();
    }

}
